import redis
from spider.base.filter import BaseFilter


class RedisFilter(BaseFilter):
    def _save(self, hash_value):
        return self.storage.sadd(self.redis_key, hash_value)

    def _is_exists(self, hash_value):
        return self.storage.sismember(self.redis_key, hash_value)

    def _get_storage(self):
        pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
        client = redis.StrictRedis(connection_pool=pool)
        return client


if __name__ == '__main__':
    f = RedisFilter(redis_key='test')

    # data = ["111", "qwe", "222", "333", "111", "qwe", "中文"]
    #
    # for d in data:
    #     if not f.is_exists(d):
    #         f.save(d)
    #     else:
    #         print('发现重复的数据：', d)
    # 'https://www.zhihu.com/question/431861103/answer/1752453075'

    print(f.storage.smembers('431861103'))